package com.rede.didiok.common.manager.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.TypeReference;
import com.rede.didiok.common.dto.community.MomentCommentDto;
import com.rede.didiok.common.dto.community.UserMomentDto;
import com.rede.didiok.common.dto.rank.MemberCommentDto;
import com.rede.didiok.common.dto.rank.RankMemberDto;
import com.rede.didiok.common.enums.ResourceTypeEnum;
import com.rede.didiok.common.exception.exceptionType.FeignException;
import com.rede.didiok.common.exception.exceptionType.InsertException;
import com.rede.didiok.common.feign.CommunityFeignClient;
import com.rede.didiok.common.feign.RankFeignClient;
import com.rede.didiok.common.manager.CommonManager;
import com.rede.didiok.common.utils.R;


/**
 * 通用模块业务处理
 *
 * @author 陌溪
 * @date 2022年3月27日23:24:06
 */
@Service("commonManager")
public class CommonManagerImpl implements CommonManager {

    @Resource
    private CommunityFeignClient communityFeignClient;
    @Resource
    private RankFeignClient rankFeignClient;


    @Override
    public String getUserUidByResource(ResourceTypeEnum resourceType, String resourceUid) {
        String userUid = "";
        switch (resourceType) {
            case RANK_MEMBER: {
                R memberResult = rankFeignClient.getRankMemberById(resourceUid);
                RankMemberDto memberDtoTemp;
                if (memberResult.getCode() == 0) {
                    memberDtoTemp = memberResult.getData(new TypeReference<RankMemberDto>(){});
                }else {
                    throw new FeignException("feign调用异常（根据id获取榜单成员）：失败");
                }
                if (memberDtoTemp != null) {
                    userUid = memberDtoTemp.getCreateUserUid();
                }
            }
            break;
            case MEMBER_COMMENT: {
                R memberCommentResult = rankFeignClient.getCommentById(resourceUid);
                MemberCommentDto memberCommentDto;
                if (memberCommentResult.getCode() == 0) {
                    memberCommentDto = memberCommentResult.getData(new TypeReference<MemberCommentDto>(){});
                }else {
                    throw new FeignException("feign调用异常（根据id获取榜单成员评论）：失败");
                }
                if (memberCommentDto != null) {
                    userUid = memberCommentDto.getCreateUserUid();
                }
            }
            break;
            case MOMENT: {
                R momentResult = communityFeignClient.getUserMomentById(resourceUid);
                UserMomentDto userMomentDto;
                if (momentResult.getCode() == 0) {
                    userMomentDto = momentResult.getData(new TypeReference<UserMomentDto>(){});
                }else {
                    throw new FeignException("feign调用异常（根据id获取榜单成员评论）：失败");
                }
                if (userMomentDto != null) {
                    userUid = userMomentDto.getCreateUserUid();
                }
            }
            break;
            case MOMENT_COMMENT: {
                R momentCommentResult = communityFeignClient.getCommentById(resourceUid);
                MomentCommentDto momentCommentDto;
                if (momentCommentResult.getCode() == 0) {
                    momentCommentDto = momentCommentResult.getData(new TypeReference<MomentCommentDto>(){});
                }else {
                    throw new FeignException("feign调用异常（根据id获取榜单成员评论）：失败");
                }
                if (momentCommentDto != null) {
                    userUid = momentCommentDto.getCreateUserUid();
                }
            }
            break;
            default:
                throw new InsertException("类型转换异常: resourceType:" + resourceType);
        }
        return userUid;
    }
}
